package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Analizaxrespuesta;
import com.lostreseditores.sabersai.model.dto.AnalizaxrespuestaDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class AnalizaxrespuestaView {
	private InputText txtAnaxresc1t;
	private InputText txtAnaxresc2t;
	private InputText txtAnaxresc3t;
	private InputText txtAnaxresc4t;
    private InputText txtAnaxresc5t;
    private InputText txtAnaxresc6t;
    private InputText txtAnaxresc7t;
    private InputText txtAnaxresestadon;
    private InputText txtPremidn_Premarcado;
    private InputText txtAnaxresidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<AnalizaxrespuestaDTO> data;
    private AnalizaxrespuestaDTO selectedAnalizaxrespuesta;

    public AnalizaxrespuestaView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            AnalizaxrespuestaDTO analizaxrespuestaDTO = (AnalizaxrespuestaDTO) e.getObject();

            if (txtAnaxresc1t == null) {
                txtAnaxresc1t = new InputText();
            }

            txtAnaxresc1t.setValue(analizaxrespuestaDTO.getAnaxresc1t());

            if (txtAnaxresc2t == null) {
                txtAnaxresc2t = new InputText();
            }

            txtAnaxresc2t.setValue(analizaxrespuestaDTO.getAnaxresc2t());

            if (txtAnaxresc3t == null) {
                txtAnaxresc3t = new InputText();
            }

            txtAnaxresc3t.setValue(analizaxrespuestaDTO.getAnaxresc3t());

            if (txtAnaxresc4t == null) {
                txtAnaxresc4t = new InputText();
            }

            txtAnaxresc4t.setValue(analizaxrespuestaDTO.getAnaxresc4t());

            if (txtAnaxresc5t == null) {
                txtAnaxresc5t = new InputText();
            }

            txtAnaxresc5t.setValue(analizaxrespuestaDTO.getAnaxresc5t());

            if (txtAnaxresc6t == null) {
                txtAnaxresc6t = new InputText();
            }

            txtAnaxresc6t.setValue(analizaxrespuestaDTO.getAnaxresc6t());

            if (txtAnaxresc7t == null) {
                txtAnaxresc7t = new InputText();
            }

            txtAnaxresc7t.setValue(analizaxrespuestaDTO.getAnaxresc7t());

            if (txtAnaxresestadon == null) {
                txtAnaxresestadon = new InputText();
            }

            txtAnaxresestadon.setValue(analizaxrespuestaDTO.getAnaxresestadon());

            if (txtPremidn_Premarcado == null) {
                txtPremidn_Premarcado = new InputText();
            }

            txtPremidn_Premarcado.setValue(analizaxrespuestaDTO.getPremidn_Premarcado());

            if (txtAnaxresidn == null) {
                txtAnaxresidn = new InputText();
            }

            txtAnaxresidn.setValue(analizaxrespuestaDTO.getAnaxresidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtAnaxresc1t != null) {
            txtAnaxresc1t.setValue(null);
            txtAnaxresc1t.setDisabled(true);
        }

        if (txtAnaxresc2t != null) {
            txtAnaxresc2t.setValue(null);
            txtAnaxresc2t.setDisabled(true);
        }

        if (txtAnaxresc3t != null) {
            txtAnaxresc3t.setValue(null);
            txtAnaxresc3t.setDisabled(true);
        }

        if (txtAnaxresc4t != null) {
            txtAnaxresc4t.setValue(null);
            txtAnaxresc4t.setDisabled(true);
        }

        if (txtAnaxresc5t != null) {
            txtAnaxresc5t.setValue(null);
            txtAnaxresc5t.setDisabled(true);
        }

        if (txtAnaxresc6t != null) {
            txtAnaxresc6t.setValue(null);
            txtAnaxresc6t.setDisabled(true);
        }

        if (txtAnaxresc7t != null) {
            txtAnaxresc7t.setValue(null);
            txtAnaxresc7t.setDisabled(true);
        }

        if (txtAnaxresestadon != null) {
            txtAnaxresestadon.setValue(null);
            txtAnaxresestadon.setDisabled(true);
        }

        if (txtPremidn_Premarcado != null) {
            txtPremidn_Premarcado.setValue(null);
            txtPremidn_Premarcado.setDisabled(true);
        }

        if (txtAnaxresidn != null) {
            txtAnaxresidn.setValue(null);
            txtAnaxresidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Analizaxrespuesta entity = null;

        try {
            Integer anaxresidn = new Integer(txtAnaxresidn.getValue().toString());
            entity = BusinessDelegatorView.getAnalizaxrespuesta(anaxresidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtAnaxresc1t.setDisabled(false);
            txtAnaxresc2t.setDisabled(false);
            txtAnaxresc3t.setDisabled(false);
            txtAnaxresc4t.setDisabled(false);
            txtAnaxresc5t.setDisabled(false);
            txtAnaxresc6t.setDisabled(false);
            txtAnaxresc7t.setDisabled(false);
            txtAnaxresestadon.setDisabled(false);
            txtPremidn_Premarcado.setDisabled(false);
            txtAnaxresidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtAnaxresc1t.setValue(entity.getAnaxresc1t());
            txtAnaxresc1t.setDisabled(false);
            txtAnaxresc2t.setValue(entity.getAnaxresc2t());
            txtAnaxresc2t.setDisabled(false);
            txtAnaxresc3t.setValue(entity.getAnaxresc3t());
            txtAnaxresc3t.setDisabled(false);
            txtAnaxresc4t.setValue(entity.getAnaxresc4t());
            txtAnaxresc4t.setDisabled(false);
            txtAnaxresc5t.setValue(entity.getAnaxresc5t());
            txtAnaxresc5t.setDisabled(false);
            txtAnaxresc6t.setValue(entity.getAnaxresc6t());
            txtAnaxresc6t.setDisabled(false);
            txtAnaxresc7t.setValue(entity.getAnaxresc7t());
            txtAnaxresc7t.setDisabled(false);
            txtAnaxresestadon.setValue(entity.getAnaxresestadon());
            txtAnaxresestadon.setDisabled(false);
            txtPremidn_Premarcado.setValue(entity.getPremarcado().getPremidn());
            txtPremidn_Premarcado.setDisabled(false);
            txtAnaxresidn.setValue(entity.getAnaxresidn());
            txtAnaxresidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveAnalizaxrespuesta(FacesUtils.checkString(
                    txtAnaxresc1t), FacesUtils.checkString(txtAnaxresc2t),
                FacesUtils.checkString(txtAnaxresc3t),
                FacesUtils.checkString(txtAnaxresc4t),
                FacesUtils.checkString(txtAnaxresc5t),
                FacesUtils.checkString(txtAnaxresc6t),
                FacesUtils.checkString(txtAnaxresc7t),
                FacesUtils.checkInteger(txtAnaxresestadon),
                FacesUtils.checkInteger(txtAnaxresidn),
                FacesUtils.checkInteger(txtPremidn_Premarcado));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteAnalizaxrespuesta(FacesUtils.checkInteger(
                    txtAnaxresidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateAnalizaxrespuesta(FacesUtils.checkString(
                    txtAnaxresc1t), FacesUtils.checkString(txtAnaxresc2t),
                FacesUtils.checkString(txtAnaxresc3t),
                FacesUtils.checkString(txtAnaxresc4t),
                FacesUtils.checkString(txtAnaxresc5t),
                FacesUtils.checkString(txtAnaxresc6t),
                FacesUtils.checkString(txtAnaxresc7t),
                FacesUtils.checkInteger(txtAnaxresestadon),
                FacesUtils.checkInteger(txtAnaxresidn),
                FacesUtils.checkInteger(txtPremidn_Premarcado));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtAnaxresidn == null) {
                txtAnaxresidn = new InputText();
            }

            txtAnaxresidn.setValue(selectedAnalizaxrespuesta.getAnaxresidn());

            BusinessDelegatorView.deleteAnalizaxrespuesta(FacesUtils.checkInteger(
                    txtAnaxresidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedAnalizaxrespuesta);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String anaxresc1t, String anaxresc2t,
        String anaxresc3t, String anaxresc4t, String anaxresc5t,
        String anaxresc6t, String anaxresc7t, Integer anaxresestadon,
        Integer anaxresidn, Integer premidn_Premarcado)
        throws Exception {
        try {
            BusinessDelegatorView.updateAnalizaxrespuesta(anaxresc1t,
                anaxresc2t, anaxresc3t, anaxresc4t, anaxresc5t, anaxresc6t,
                anaxresc7t, anaxresestadon, anaxresidn, premidn_Premarcado);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("AnalizaxrespuestaView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtAnaxresc1t() {
        return txtAnaxresc1t;
    }

    public void setTxtAnaxresc1t(InputText txtAnaxresc1t) {
        this.txtAnaxresc1t = txtAnaxresc1t;
    }

    public InputText getTxtAnaxresc2t() {
        return txtAnaxresc2t;
    }

    public void setTxtAnaxresc2t(InputText txtAnaxresc2t) {
        this.txtAnaxresc2t = txtAnaxresc2t;
    }

    public InputText getTxtAnaxresc3t() {
        return txtAnaxresc3t;
    }

    public void setTxtAnaxresc3t(InputText txtAnaxresc3t) {
        this.txtAnaxresc3t = txtAnaxresc3t;
    }

    public InputText getTxtAnaxresc4t() {
        return txtAnaxresc4t;
    }

    public void setTxtAnaxresc4t(InputText txtAnaxresc4t) {
        this.txtAnaxresc4t = txtAnaxresc4t;
    }

    public InputText getTxtAnaxresc5t() {
        return txtAnaxresc5t;
    }

    public void setTxtAnaxresc5t(InputText txtAnaxresc5t) {
        this.txtAnaxresc5t = txtAnaxresc5t;
    }

    public InputText getTxtAnaxresc6t() {
        return txtAnaxresc6t;
    }

    public void setTxtAnaxresc6t(InputText txtAnaxresc6t) {
        this.txtAnaxresc6t = txtAnaxresc6t;
    }

    public InputText getTxtAnaxresc7t() {
        return txtAnaxresc7t;
    }

    public void setTxtAnaxresc7t(InputText txtAnaxresc7t) {
        this.txtAnaxresc7t = txtAnaxresc7t;
    }

    public InputText getTxtAnaxresestadon() {
        return txtAnaxresestadon;
    }

    public void setTxtAnaxresestadon(InputText txtAnaxresestadon) {
        this.txtAnaxresestadon = txtAnaxresestadon;
    }

    public InputText getTxtPremidn_Premarcado() {
        return txtPremidn_Premarcado;
    }

    public void setTxtPremidn_Premarcado(InputText txtPremidn_Premarcado) {
        this.txtPremidn_Premarcado = txtPremidn_Premarcado;
    }

    public InputText getTxtAnaxresidn() {
        return txtAnaxresidn;
    }

    public void setTxtAnaxresidn(InputText txtAnaxresidn) {
        this.txtAnaxresidn = txtAnaxresidn;
    }

    public List<AnalizaxrespuestaDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataAnalizaxrespuesta();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<AnalizaxrespuestaDTO> analizaxrespuestaDTO) {
        this.data = analizaxrespuestaDTO;
    }

    public AnalizaxrespuestaDTO getSelectedAnalizaxrespuesta() {
        return selectedAnalizaxrespuesta;
    }

    public void setSelectedAnalizaxrespuesta(
        AnalizaxrespuestaDTO analizaxrespuesta) {
        this.selectedAnalizaxrespuesta = analizaxrespuesta;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
